package ua.soso.service.impl;

import org.springframework.beans.factory.annotation.Required;
import ua.soso.dao.UserDao;
import ua.soso.dao.model.User;
import ua.soso.service.UserService;
import ua.soso.service.bean.UserBean;

/**
 * Created by Babka Aleksey
 * Date: 20.04.12
 */
public class UserServiceImpl implements UserService {
    private UserDao userDao;

    @Required
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public UserBean getUser(String login, String password) {
        User user = userDao.findByLoginAndPass(login, password);

        if (user != null) {
            UserBean userBean = new UserBean();

            userBean.setName(user.getName());
            userBean.setId(user.getId());

            return userBean;
        } else {
            return null;
        }
    }

    @Override
    public boolean saveUser(User user) {
        User byLogin = userDao.findByLogin(user.getLogin());

        if (byLogin == null) {
            userDao.saveUser(user);

            return true;
        }

        return false;
    }
}
